package com.hoshiicloud.shxping.jobhandler;

import com.alibaba.fastjson.JSON;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.saleorder.entity.OrderMain;
import com.hoshiicloud.shxping.dto.PlatStoreDTO;
import com.hoshiicloud.shxping.feign.IUserFeignService;
import com.hoshiicloud.shxping.feign.OrderFeignService;
import com.hoshiicloud.shxping.feign.VeriCenterFeignService;
import com.hoshiicloud.shxping.param.StoreOrderQueryParam;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yangcheng
 * @version 1.0
 * @title: NightOrderStatisticsNotifyHandler
 * @description: 晚上订单统计通知任务
 * @date 2019/8/30 12:04
 * @mail maiboyc@163.com
 */
@Slf4j
@JobHandler("nightOrderStatisticsNotifyHandler")
@Component
public class NightOrderStatisticsNotifyHandler extends IJobHandler {

    // 订单微服务Service
    @Autowired
    private OrderFeignService orderFeignService;

    // 验证中心微服务Service
    @Autowired
    private VeriCenterFeignService vericenterFeignservice;

    // 用户微服务Service
    @Autowired
    private IUserFeignService userFeignService;

    // 任务执行方法
    @Override
    public ReturnT<String> execute(String param) {
        // 日期格式化
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        XxlJobLogger.log("晚上订单统计通知任务开始执行，当前时间", dtf.format(LocalDateTime.now()));
        // 开始时间
        LocalDateTime beginTime = LocalDateTime.now().minusDays(1).withHour(22).withMinute(0).withSecond(0);
        // 结束时间
        LocalDateTime endTime = LocalDateTime.now();
        // 支付状态
        Integer status = 3;
        // 查询订单数据
        StoreOrderQueryParam queryParam = new StoreOrderQueryParam();
        queryParam.setBeginTime(beginTime);
        queryParam.setEndTime(endTime);
        queryParam.setStatus(status);
        Response<List<OrderMain>> list = orderFeignService.listCanPickUpOrderWithTime(queryParam);
        if (null != list && list.getCode() == 200 && null != list.getData()) {
            // 根据店铺分组
            Map<Long,List<OrderMain>> map = list.getData().stream().collect(Collectors.groupingBy(OrderMain::getStoreId));
            map.keySet().forEach(item -> {
                // 查找对应店铺联系电话
                Response<PlatStoreDTO> storeResponse = userFeignService.queryById(item);
                if (null != storeResponse && storeResponse.getCode() == 200 && null != storeResponse.getData()) {
                    if (map.get(item).size() > 0) {
                        // 模板参数
                        Map<String,Object> telMap = new HashMap<>();
                        telMap.put("count",String.valueOf(map.get(item).size()));
                        // 调用短信发送服务，发送提醒短信
                        Response send_rs = vericenterFeignservice.sendTplMsg(storeResponse.getData().getMobile(), JSON.toJSONString(telMap),"3177334");
                        if (null != send_rs && send_rs.getCode() == 200 && null != send_rs.getData()) {
                            XxlJobLogger.log("给【"+storeResponse.getData().getCharge()+"】发送订单统计短信成功");
                        } else {
                            XxlJobLogger.log("给【"+storeResponse.getData().getCharge()+"】发送订单统计短信失败");
                        }
                    }
                } else {
                    XxlJobLogger.log("查询对应的店铺信息失败，ID为：",item);
                }
            });
        } else {
            XxlJobLogger.log("没有查询到订单数据");
            return ReturnT.FAIL;
        }
        XxlJobLogger.log("晚上订单统计通知任务结束执行，当前时间", dtf.format(LocalDateTime.now()));
        return SUCCESS;
    }

}
